package com.aerfafish.spider.neo4j.spider.service.impl;

import com.aerfafish.spider.neo4j.dao.PersonManage;
import com.aerfafish.spider.neo4j.repository.FundRepository;
import com.aerfafish.spider.neo4j.repository.PersonManagerRepository;
import com.aerfafish.spider.neo4j.repository.PersonRepository;
import com.aerfafish.spider.neo4j.spider.service.PersonFundRelationService;
import org.neo4j.driver.exceptions.TransientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.stereotype.Service;

/**
 * @author aerfafish
 * @date 2021/4/21 11:12 上午
 */

@Service
public class PersonFundRelationServiceImpl implements PersonFundRelationService {

    @Autowired
    PersonManagerRepository personManagerRepository;

    @Autowired
    PersonRepository personRepository;

    @Autowired
    FundRepository fundRepository;

    @Override
    public void save(PersonManage personManage) throws Exception {
        save(personManage.getPerson().getCode(), personManage.getFund().getCode());
    }

    @Override
    public void save(String managerCode, String fundCode) throws Exception {

        try {
            personManagerRepository.save(managerCode, fundCode);
        } catch (TransientException | InvalidDataAccessApiUsageException e) {
            try {
                personManagerRepository.save(managerCode, fundCode);
            } catch (TransientException | InvalidDataAccessApiUsageException e0) {
                personManagerRepository.save(managerCode, fundCode);
            }
        }

//        if (relationships.size() > 1) {
//            List<Person> people = personRepository.queryPersonByCode(managerCode);
//            if (people.size() > 1) {
//                String peopleCode = "";
//                for (Person person : people) {
//                    peopleCode = peopleCode.concat(person.getCode() + " ");
//                }
//                throw new Exception("managerCode repeated " + peopleCode);
//            }
//            List<Fund> funds = fundRepository.queryFundByCode(fundCode);
//            if (funds.size() > 1) {
//                String fundsCode = "";
//                for (Fund fund : funds) {
//                    fundsCode = fundsCode.concat(fund.getCode() + " ");
//                }
//                throw new Exception("fundCode repeated " + fundsCode);
//            }
//        }
    }
}
